<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgpu - instancing morph target animations</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link type="text/css" rel="stylesheet" href="example.css">
	</head>
	<body>

		<div id="info">
			<a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>

			<div class="title-wrapper">
				<a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Instancing Morph</span>
			</div>

			<small>
				Rendering instances which are individually animated via morph targets.
			</small>
		</div>

		<script type="importmap">
			{
				"imports": {
					"three": "../build/three.webgpu.js",
					"three/webgpu": "../build/three.webgpu.js",
					"three/tsl": "../build/three.tsl.js",
					"three/addons/": "./jsm/"
				}
			}
		</script>

		<script type="module">

			import * as THREE from 'three/webgpu';

			import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
			import { Inspector } from 'three/addons/inspector/Inspector.js';

			let camera, scene, renderer, mesh, mixer, dummy;

			const offset = 5000;

			const timeOffsets = new Float32Array( 1024 );

			for ( let i = 0; i < 1024; i ++ ) {

				timeOffsets[ i ] = Math.random() * 3;

			}

			const clock = new THREE.Clock( true );

			init();

			function init() {

				camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 100, 10000 );

				scene = new THREE.Scene();

				scene.background = new THREE.Color( 0x99DDFF );

				scene.fog = new THREE.Fog( 0x99DDFF, 5000, 10000 );

				//

				const light = new THREE.DirectionalLight( 0xffffff, 1 );

				light.position.set( 200, 1000, 50 );

				light.shadow.mapSize.width = 2048;
				light.shadow.mapSize.height = 2048;
				light.castShadow = true;

				light.shadow.camera.left = - 5000;
				light.shadow.camera.right = 5000;
				light.shadow.camera.top = 5000;
				light.shadow.camera.bottom = - 5000;
				light.shadow.camera.far = 2000;

				light.shadow.bias = - 0.01;

				light.shadow.camera.updateProjectionMatrix();

				scene.add( light );

				const hemi = new THREE.HemisphereLight( 0x99DDFF, 0x669933, 1 / 3 );

				scene.add( hemi );

				const ground = new THREE.Mesh(
					new THREE.PlaneGeometry( 1000000, 1000000 ),
					new THREE.MeshStandardMaterial( { color: 0x669933 } )
				);

				ground.rotation.x = - Math.PI / 2;

				ground.receiveShadow = true;

				scene.add( ground );

				const loader = new GLTFLoader();

				loader.load( 'models/gltf/Horse.glb', function ( glb ) {

					dummy = glb.scene.children[ 0 ];

					mesh = new THREE.InstancedMesh( dummy.geometry, new THREE.MeshStandardNodeMaterial( {
						flatShading: true,
					} ), 1024 );

					mesh.castShadow = true;

					for ( let x = 0, i = 0; x < 32; x ++ ) {

						for ( let y = 0; y < 32; y ++ ) {

							dummy.position.set( offset - 300 * x + 200 * Math.random(), 0, offset - 300 * y );

							dummy.updateMatrix();

							mesh.setMatrixAt( i, dummy.matrix );

							mesh.setColorAt( i, new THREE.Color( `hsl(${Math.random() * 360}, 50%, 66%)` ) );

							i ++;
			
						}

			
					}

					scene.add( mesh );

					mixer = new THREE.AnimationMixer( glb.scene );

					const action = mixer.clipAction( glb.animations[ 0 ] );

					action.play();
			
				} );


				// renderer

				renderer = new THREE.WebGPURenderer( { antialias: true } );
				renderer.setAnimationLoop( animate );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.shadowMap.enabled = true;
				renderer.inspector = new Inspector();
				document.body.appendChild( renderer.domElement );

				window.addEventListener( 'resize', onWindowResize );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function animate() {

				const time = clock.getElapsedTime();

				const r = 3000;
				camera.position.set( Math.sin( time / 10 ) * r, 1500 + 1000 * Math.cos( time / 5 ), Math.cos( time / 10 ) * r );
				camera.lookAt( 0, 0, 0 );

				if ( mesh ) {

					for ( let i = 0; i < 1024; i ++ ) {

						mixer.setTime( time + timeOffsets[ i ] );

						mesh.setMorphAt( i, dummy );

					}

					mesh.morphTexture.needsUpdate = true;

				}

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
